package SV_STRUTS_NOTVALID;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Example {
    public class SV_STRUTS_NOTVALID_Sample_1 extends ActionForm {
        private String name;
        private String birthdayString;
        protected Date birthday;
        public ActionErrors validate(ActionMapping map,
                                     HttpServletRequest req) {
            ActionErrors errors = new ActionErrors();
            SimpleDateFormat dateConvertor = new SimpleDateFormat(
                    req.getParameter("date.format"));
            try {
                birthday = dateConvertor.parse(birthdayString);
            } catch (ParseException e) {
                errors.add(ActionErrors.GLOBAL_MESSAGE,
                        new ActionMessage("Bad date"));
            }
            return super.validate(map, req);
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getBirthday() {
            return birthdayString;
        }
        public void setBirthday(String birthday) {
            this.birthdayString = birthday;
        }
    }
}
